package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.erpPicking_report.ErpPickingReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.reportUtils.LKingSortUtil;
import com.navi.mould1.service.IErpPickingReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static java.util.Comparator.comparing;

@Slf4j
@Service
public class ErpPickingReportServiceImpl implements IErpPickingReportService {


    public List<ErpPickingReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("A.pick_timestamp,");
        sqlVo.appendSql("A.pick_timestamp as pick_date,");
        sqlVo.appendSql("A.pick_timestamp as pick_time,");
        sqlVo.appendSql("A.pick_stat,");
        sqlVo.appendSql("A.pick_id,");
        sqlVo.appendSql("A.pick_cate,");
        sqlVo.appendSql("A.note,");
        sqlVo.appendSql("A.evt_usr,");
        sqlVo.appendSql("G.usr_name as evt_usr_name,");
        sqlVo.appendSql("A.evt_cate,");
        sqlVo.appendSql("B.fab_id_fk,");
        sqlVo.appendSql("b_1.ext_1 as fab_id,");
        sqlVo.appendSql("B.mtrl_prod_id_fk,");
        sqlVo.appendSql("b_2.mtrl_name,");
        sqlVo.appendSql("b_2.mtrl_spec,");
        sqlVo.appendSql("b_2.mtrl_unit,");
        sqlVo.appendSql("B.mtrl_qty");
        sqlVo.appendSql("FROM");
        sqlVo.appendSql("ret_pickm_info A");
        sqlVo.appendSql("INNER JOIN ret_wo_mtrl B ON A.pick_id = B.pick_id");
        sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc,ext_1 from icim_jn.bis_data where data_cate = 'LSWH' and data_id='5') b_1 ON B.fab_id_fk = b_1.data_ext");
        sqlVo.appendSql("LEFT JOIN bis_mtrl b_2 on B.mtrl_prod_id_fk = b_2.mtrl_code");
        sqlVo.appendSql("LEFT JOIN bis_user G ON A.evt_usr = G.usr_id");
        sqlVo.appendSql("WHERE A.pick_stat = 'COMP'");
        if (StringUtils.isNotEmpty(conditions.getMtrl_fab_id())) sqlVo.appendSql("AND B.FAB_ID_FK in" + jointInSql(conditions.getMtrl_fab_id()));
        if (StringUtils.isNotEmpty(conditions.getMtrl_prod_id_fk())) sqlVo.appendSql("AND B.MTRL_PROD_ID_FK in" + jointInSql(conditions.getMtrl_prod_id_fk()));
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.pick_timestamp >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.pick_timestamp <= ?", conditions.getEnd_time());
        sqlVo.appendSql("ORDER BY A.pick_timestamp");

        List<ErpPickingReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, ErpPickingReportGridOut.class);

        //by mtrlProdId的汇总数据
        if ("SUMMARY".equals(conditions.getSummary_or_detail())) {
            List<ErpPickingReportGridOut> by_mtrlPordId_fab_gridOut_list = new ArrayList<>();
            resultDataList.stream().collect(Collectors.groupingBy(data -> data.getMtrl_prod_id_fk())).forEach((key, list) -> {
                ErpPickingReportGridOut gridOut = new ErpPickingReportGridOut();
                //gridOut.setPick_date(list.stream().collect(minBy(comparing(data -> data.getPick_date()))).orElseGet(ErpPickingReportGridOut::new).getPick_date());
                //gridOut.setFab_id(list.get(0).getFab_id());
                //gridOut.setFab_id_fk(list.get(0).getFab_id_fk());
                //gridOut.setPick_stat(list.get(0).getPick_stat());
                gridOut.setMtrl_prod_id_fk(list.get(0).getMtrl_prod_id_fk());
                gridOut.setMtrl_name(list.get(0).getMtrl_name());
                gridOut.setMtrl_spec(list.get(0).getMtrl_spec());
                gridOut.setMtrl_unit(list.get(0).getMtrl_unit());
                gridOut.setMtrl_qty(String.valueOf(list.stream().mapToDouble(data -> Double.parseDouble(data.getMtrl_qty())).sum()));
                by_mtrlPordId_fab_gridOut_list.add(gridOut);
            });
            LKingSortUtil.sort(by_mtrlPordId_fab_gridOut_list, new String[] { "mtrl_prod_id_fk", "fab_id", "pick_timestamp" }, new boolean[] { false, true });
            return by_mtrlPordId_fab_gridOut_list;
        }

        return mySql_QueryRunner_QueryListBySql(sqlVo, ErpPickingReportGridOut.class);
    }



}
